package cn.TS.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnDB {

    private static final Logger LOG = LoggerFactory.getLogger(ConnDB.class);


    public Connection conn = null;
    public Statement stmt = null;
    public ResultSet rs = null;
    private static String propFileName = "connDB.properties";
    private static Properties prop = new Properties();
    private static String dbClassName = "com.mysql.Driver";
    private static String dbUrl = "jdbc:mysql://localhost:3306/db_login?user=root&&password=123456&&characterEncoding=utf-8";

    public ConnDB() {
        try {
            InputStream in = getClass().getResourceAsStream(propFileName);
            prop.load(in);
            dbClassName = prop.getProperty("DB_CLASS_NAME");
            dbUrl = prop.getProperty("DB_URL", dbUrl);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(dbClassName).newInstance();
            conn = DriverManager.getConnection(dbUrl);
        } catch (Exception ee) {
            // TODO: handle exception
            ee.printStackTrace();
        }
        if (conn == null) {
            //System.out.println("����: ConnDB.getConnection()��ȡʧ��" + dbClassName + "\r\n����λ��:" + dbUrl);
            // 不要用sout 用LOG
            LOG.error("ConnDB.getConnection() failed, dbClassName: {}, dburl: {} ", dbClassName, dbUrl);

        }
        return conn;
    }

    public ResultSet executeQuety(String sql) {
        try {
            conn = getConnection();
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
        } catch (SQLException ex) {
            // TODO: handle exception
            System.err.println(ex.getMessage());
        }
        return rs;
    }

    public int executeUpdate(String sql) {
        int result = 0;
        try {
            conn = getConnection();
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            // TODO: handle exception
            result = 0;
        }

        return result;
    }

    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace(System.err);
        }
    }


}
